package com.baizhi.service.impl;

import com.baizhi.Annotation.VideoRedis;
import com.baizhi.dao.VideoDao;
import com.baizhi.entity.Video;
import com.baizhi.feign.CategoryFeign;
import com.baizhi.feign.UserFeign;
import com.baizhi.po.UserPO;
import com.baizhi.po.VideoPO;
import com.baizhi.po.VideosPO;
import com.baizhi.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频(Video)表服务实现类
 *
 * @author makejava
 * @since 2021-09-24 12:43:06
 */
@Service("videoService")
@Transactional
public class VideoServiceImpl extends ServiceImpl<VideoDao, Video> implements VideoService {

    @Autowired
    private VideoDao videoDao;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private CategoryFeign categoryFeign;


    @Override
    @VideoRedis //添加切面
    public VideoPO PageQueryAll(Integer page, Integer per_page, Integer id, String name, Integer category_id, String uploader_name) {
        //创建返回对象
        VideoPO videoPO = new VideoPO();

        //分页查询
        Page<Video> pages = new Page<>();
        //创建条件对象
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        //创建视频id条件
        if (id != null) {
            queryWrapper.eq(Video::getId, id);
        }
        //创建视频标题条件
        if (name != null) {
            queryWrapper.eq(Video::getTitle, name);
        }
        //分类id查所有视频
        if (category_id != null) {
            queryWrapper.eq(Video::getCategoryId, category_id);
        }
        //up主名称
        if (uploader_name != null) {
            //调用用户查询用户对应的id,在根据用户id查取对应的视频
            UserPO userPO = userFeign.queryByName(uploader_name);
            Integer uploaderId = userPO.getId();
            queryWrapper.eq(Video::getUid, uploaderId);
        }

        //创建分页对象
        Page<Video> videoPage = videoDao.selectPage(pages, queryWrapper);


        //获取视频数据
        List<Video> records = videoPage.getRecords();
        ArrayList<VideosPO> objects = new ArrayList<>();

        //组装前段显示
        for (Video record : records) {
            //创建集合单个对象
            VideosPO videosPO = new VideosPO();
            //拷贝到videosPO
            BeanUtils.copyProperties(record, videosPO);
            //给没有数据的赋值
            //获取Category名字
            String s = categoryFeign.queryById(record.getCategoryId());
            videosPO.setCategory(s);
            //获取ip主
            UserPO userPO1 = userFeign.queryById(record.getUid());
            videosPO.setUploader(userPO1);
            objects.add(videosPO);
        }
        //获取总条数
        long total = videoPage.getTotal();
        System.out.println("total = " + total);
        videoPO.setTotal_count(total);

        videoPO.setItems(objects);
        return videoPO;
    }
}

